<?php


namespace app\mobile\controller;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept");
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');

use think\facade\Config;
use think\Request;
use think\Db;

class User extends \Common1 {

    public $token = "";

    public function __construct(Request $request = null){
        parent::__construct();
        if(\request()->action() == "bindkey" ||
            \request()->action() == "getmoney" ||
            \request()->action() == "withdraw" ||
            \request()->action() == "bindphone" ||
            \request()->action() == "getnums" ||
            \request()->action() == "getbaseinfo" ||
            \request()->action() == "updatebaseinfo" ||
            \request()->action() == "getvips"
        ){
            $arr = getallheaders();
            $token = $this->Check(isset($arr["Authorization"]) ? $arr["Authorization"] : "");
            $this->token = $token;
        }
    }

    // 发送验证码
    public function getCode(){
        if(isset($_POST["phone"]) && isset($_POST["type"])){
            // type: 1 注册 2 登录 3 绑定手机号
            $country = (isset($_POST["country_code"]) ? $_POST["country_code"] : "+86");
            switch ($country){
                case "+86" : $state = 1; break;
                default : $state = 2; break;
            }
            $phone = $_POST["phone"];
            $type = $_POST["type"];
            $code = rand(1111, 9999);
            $up = [
                "state" => 1
            ];
            Db::name("phone_code")->where("phone", $phone)->where("state", 0)->update($up);
            $userCheck = Db::name("user_buy")->where("tel2", $phone)->find();
            if(empty($userCheck)){
                $user = Db::name("user_buy")->where("tel1", $phone)->where("password != ''")->find();
                $user1 = Db::name("user_buy")->where("tel1", $phone)->where("openid != ''")->find();
                switch ($type){
                    case 1: !empty($user) ? Code(403, "用户已经注册,请前往登录界面") : false; break;
                    case 3: !empty($user1) ? Code(403, "该手机号已经被绑定，请核对") : false; break;
                }
                $add = [
                    "phone" => $phone,
                    "code" => $code,
                    "time" => time()
                ];
                Db::name("phone_code")->insert($add);
                $return = Sms::sendSms($phone, $code, $type, $state, $country);
                $arr = json_decode(json_encode($return),true);
                if($arr["Code"] == "OK"){
                    $data = (Object)[];
                    Code(200, "验证码发送成功", $data);
                }else{
                    Code(403, "服务器错误，请稍后再试");
                }
            }else{
                $msg = "";
                switch ($type){
                    case 1 : case 3 : $msg = "号码已经存在，请核对"; break;
                    case 2 : $msg = "对不起，该号码为备用号码，请用绑定手机号登录"; break;
                }
                Code(403, $msg);
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 登录
    public function Login(){
        if(isset($_POST["phone"])){
            $user = Db::name("user_buy")->where("tel1", $_POST["phone"])->find();
            $number = Db::name("num")->where("ub_id", $user["ub_id"])->select();
            $nums = [];
            if(empty($user) || $user["password"] == ""){
                $u = Db::name("user_buy")->where("tel2", $_POST["phone"])->find();
                if(empty($u)){
                    Code(403, "尚未注册,请前去注册");
                }else{
                    Code(403, "请用手机号1进行登录");
                }
            }else{
                if(!empty($number)){
                    foreach ($number as $value){
                        $admin = Db::name("admin")->where("aid", $value["aid"])->find();
                        $nums[] = [
                            "sub_id" => (int)$admin["aid"],
                            "sub_name" => $admin["name"],
                            "sub_num" => (int)$value["number"]
                        ];
                    }
                }
                $arr = [
                    "time" => time(),
                    "id" => $user["ub_id"]
                ];
                $token = $this->MakeToken($arr);
                if($user["openid"] == ""){
                    $wx = false;
                }else{
                    $wx = [
                        "avatar" => $user["avatar"],
                        "nickname" => $user["nickname"]
                    ];
                }
                if(isset($_POST["code"])){
                    $code = Db::name("phone_code")->where("phone", $_POST["phone"])->where("state", 0)->find();
                    if(empty($code)){
                        Code(403, "请点击发送验证码");
                    }else if($code["code"] != $_POST["code"]){
                        Code(403, "验证码错误");
                    }else{
                        $up = [
                            "state" => 1
                        ];
                        Db::name("phone_code")->where("pc_id", $code["pc_id"])->update($up);
                        $data = [
                            "info" => [
                                "money" => "",
                                "phone" => $user["tel1"],
                                "wx" => $wx
                            ],
                            "nums" => $nums,
                            "token" => $token
                        ];
                        Code(200, "登录成功", $data);
                    }
                }else if(isset($_POST["password"])){
                    if($user["password"] == md5($_POST["password"])){
                        $data = [
                            "info" => [
                                "money" => "",
                                "phone" => $user["tel1"],
                                "wx" => $wx
                            ],
                            "nums" => $nums,
                            "token" => $token
                        ];
                        Code(200, "登录成功", $data);
                    }else{
                        Code(403, "密码错误");
                    }
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    //  注册
    public function register(){
        if(isset($_POST["phone"]) && isset($_POST["code"]) && isset($_POST["password"]) && isset($_POST["rePassword"])){
            $phone = $_POST["phone"]; $password = $_POST["password"];
            $user = Db::name("user_buy")->where("tel1", $phone)->whereOr("tel2", $phone)->find();
            if(!empty($user)){
                if($user["password"] != ""){
                    Code(403, "用户已经注册,请前往登录界面");
                }
                $code = Db::name("phone_code")->where("phone", $phone)->where("state", 0)->find();
                if(!empty($code)){
                    if($code["code"] == $_POST["code"]){
                        $update = [
                            "state" => 1
                        ];
                        Db::name("phone_code")->where("pc_id", $code["pc_id"])->update($update);

                        $info = [
                            "password" => md5($password),
                        ];
                        Db::name("user_buy")->where("ub_id", $user["ub_id"])->update($info);
                        $id = $user["ub_id"];
                        $arr = [
                            "time" => time(),
                            "id" => $id
                        ];
                        $token = $this->MakeToken($arr);
                        $data = [
                            "info" => [
                                "money" => "",
                                "phone" => $phone,
                                "wx" => false
                            ],
                            "nums" => [],
                            "token" => $token
                        ];
                        Code(200, "登录成功", $data);
                    }else{
                        Code(403, "验证码错误");
                    }
                }else{
                    Code(403, "请点击发送验证码");
                }
            }else{
                $code = Db::name("phone_code")->where("phone", $phone)->where("state", 0)->find();
                if(!empty($code)){
                    if($code["code"] == $_POST["code"]){
                        $update = [
                            "state" => 1
                        ];
                        Db::name("phone_code")->where("pc_id", $code["pc_id"])->update($update);

                        $add = [
                            "tel1" => $phone,
                            "password" => md5($password),
                        ];
                        $id = Db::name("user_buy")->insertGetId($add);
                        $up = [
                            "name" => "User_".$id,
                            "nickname" => "User_".$id
                        ];
                        Db::name("user_buy")->where("ub_id", $id)->update($up);
                        $arr = [
                            "time" => time(),
                            "id" => $id
                        ];
                        $token = $this->MakeToken($arr);
                        $data = [
                            "info" => [
                                "money" => "",
                                "phone" => $phone,
                                "wx" => false
                            ],
                            "nums" => [],
                            "token" => $token
                        ];
                        Code(200, "登录成功", $data);
                    }else{
                        Code(403, "验证码错误");
                    }
                }else{
                    Code(403, "请点击发送验证码");
                }
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }

    }

    // 检查token
    public function CheckToken(){
        if(isset($_POST["token"])){
            $token = $this->Check($_POST["token"]);
            $id = $token["id"];
            $user = Db::name("user_buy")->where("ub_id", $id)->find();
            if($user["openid"] == ""){
                $wx = false;
            }else{
                $wx = [
                    "avatar" => $user["avatar"],
                    "nickname" => $user["nickname"]
                ];
            }
            $number = Db::name("num")->where("ub_id", $id)->select();
            $nums = [];
            if(!empty($number)){
                foreach ($number as $value){
                    $admin = Db::name("admin")->where("aid", $value["aid"])->find();
                    $nums[] = [
                        "sub_id" => (int)$admin["aid"],
                        "sub_name" => $admin["name"],
                        "sub_num" => (int)$value["number"]
                    ];
                }
            }
            $arr = [
                "time" => time(),
                "id" => $user["ub_id"]
            ];
            $token = $this->MakeToken($arr);
            $data = [
                "info" => [
                    "money" => "",
                    "phone" => $user["tel1"],
                    "wx" => $wx
                ],
                "nums" => $nums,
                "token" => $token
            ];
            Code(200, "登录成功", $data);
        }
    }

    private function RemovePhote($path, $newpath, $name){
        if(!is_dir($newpath)){
            mkdir($newpath, 0777, true);
        }
        copy($path.$name, $newpath.$name);
        # 删除
        unlink($path.$name);
    }

    // 绑定手机号
    public function BindPhone(){
        $id = $this->token["id"];
        $user = Db::name("user_buy")->where("ub_id", $id)->find();
        if(isset($_POST["phone"]) && isset($_POST["code"])){
            $phone = $_POST["phone"];
            $code = Db::name("phone_code")->where("phone", $phone)->where("state", 0)->find();
            if(!empty($code)){
                if($code["code"] == $_POST["code"]){
                    $update = [
                        "state" => 1
                    ];
                    Db::name("phone_code")->where("pc_id", $code["pc_id"])->update($update);
                    $is_phone = Db::name("user_buy")->where("tel1", $phone)->find();
                    if(empty($is_phone)){
                        $update_user = [
                            "tel1" => $phone
                        ];
                        Db::name("user_buy")->where("ub_id", $id)->update($update_user);
                    }else{
                        if($is_phone["openid"] == ""){
                            $update_user = [
                                "name" => $is_phone["name"] != "" ? $is_phone["name"] : $user["name"],
                                "idcard" => $is_phone["idcard"] != "" ? $is_phone["idcard"] : $user["idcard"],
                                "tel1" => $is_phone["tel1"],
                                "tel2" => $user["tel2"] == "" ? $is_phone["tel2"] : $user["tel2"],
                                "password" => $user["password"] == "" ? $is_phone["password"] : $user["password"],
                                "country" => $user["country"] == "" ? $is_phone["country"] : $user["country"],
                                "wxid" => $user["wxid"] == "" ? $is_phone["wxid"] : $user["wxid"],
                                "email" => $user["email"] == "" ? $is_phone["email"] : $user["email"],
                                "line" => $user["line"] == "" ? $is_phone["line"] : $user["line"],
                                "postcode" => $user["postcode"] == "" ? $is_phone["postcode"] : $user["postcode"],
                                "address" => $user["address"] == "" ? $is_phone["address"] : $user["address"],
                                "job" => $user["job"] == "" ? $is_phone["job"] : $user["job"],
                                "bankname" => $user["bankname"] == "" ? $is_phone["bankname"] : $user["bankname"],
                                "bankcard" => $user["bankcard"] == "" ? $is_phone["bankcard"] : $user["bankcard"],
                                "passport" => $user["passport"] == "" ? $is_phone["passport"] : $user["passport"],
                                "photo" => $user["photo"] == "" ? $is_phone["photo"] : $user["photo"],
                                "idcardPhoto1" => $user["idcardPhoto1"] == "" ? $is_phone["idcardPhoto1"] : $user["idcardPhoto1"],
                                "idcardPhoto2" => $user["idcardPhoto2"] == "" ? $is_phone["idcardPhoto2"] : $user["idcardPhoto2"],
                            ];
                            $upuid = [
                                "ub_id" => $id
                            ];
                            Db::name("user_buy")->where("ub_id", $id)->update($update_user);

                            Db::name("user_buy_company")->where("ub_id", $is_phone["ub_id"])->update($upuid);
                            Db::name("watched")->where("ub_id", $is_phone["ub_id"])->update($upuid);
                            Db::name("num")->where("ub_id", $is_phone["ub_id"])->update($upuid);
                            Db::name("goods")->where("ub_id", $is_phone["ub_id"])->update($upuid);
                            Db::name("consigns")->where("ub_id", $is_phone["ub_id"])->update($upuid);

                            // 图片转移
                            if($is_phone["idcardPhoto1"] != ""){
                                $this->RemovePhote("./static/upload/people/idcard/buy/{$is_phone["ub_id"]}/", "./static/upload/people/idcard/buy/{$id}/", $is_phone["idcardPhoto1"]);
                            }
                            if($is_phone["idcardPhoto2"] != ""){
                                $this->RemovePhote("./static/upload/people/idcard/buy/{$is_phone["ub_id"]}/", "./static/upload/people/idcard/buy/{$id}/", $is_phone["idcardPhoto2"]);
                            }
                            if($is_phone["photo"] != ""){
                                $this->RemovePhote("./static/upload/people/photo/buy/{$is_phone["ub_id"]}/", "./static/upload/people/photo/buy/{$id}/", $is_phone["photo"]);
                            }
                            if($is_phone["passport"] != ""){
                                $this->RemovePhote("./static/upload/people/passport/buy/{$is_phone["ub_id"]}/", "./static/upload/people/passport/buy/{$id}/", $is_phone["passport"]);
                            }

                            Db::name("user_buy")->where("ub_id", $is_phone["ub_id"])->delete();
                        }else{
                            Code(403, "对不起，改号码已经被绑定");
                        }
                    }

                    $user = Db::name("user_buy")->where("ub_id", $id)->find();
                    if($user["openid"] == ""){
                        $wx = false;
                    }else{
                        $wx = [
                            "avatar" => $user["avatar"],
                            "nickname" => $user["nickname"]
                        ];
                    }
                    $data = [
                        "money" => "",
                        "phone" => $user["tel1"],
                        "wx" => $wx
                    ];
                    Code(200, "绑定成功", $data);
                }else{
                    Code(403, "验证码错误");
                }
            }else{
                Code(403, "请点击发送验证码");
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 绑定号码牌
    public function BindKey(){
        if(isset($_POST["num"]) && isset($_POST["key"]) && isset($_POST["sub_id"])){
            $id = $this->token["id"];
            $num = Db::name("num")->where("aid", $_POST["sub_id"])->where("ub_id", $id)->find();
            if(empty($num)){
                $check = Db::name("num")->where("aid", $_POST["sub_id"])->where("number", $_POST["num"])->where("ub_id", 0)->find();
                if(!empty($check)){
                    if($check["password"] == $_POST["key"]){
                        $com = Db::name("user_buy_company")->where("aid", $_POST["sub_id"])->where("ub_id", $id)->find();
                        $pay_state = $com["pay_state"];
                        if(empty($com)){
                            $insert = [
                                "ub_id" => $id,
                                "aid" => $_POST["sub_id"],
                                "reg_time" => time()
                            ];
                            $id = Db::name("user_buy_company")->insertGetId($insert);
                            $pay_state = 0;
                        }
                        if($pay_state != 0){
                            $up = [
                                "ub_id" => $id
                            ];
                            Db::name("num")->where("nid", $check["nid"])->update($up);
                            $number = Db::name("num")->where("ub_id", $id)->select();
                            $nums = [];
                            if(!empty($number)){
                                foreach ($number as $value){
                                    $admin = Db::name("admin")->where("aid", $value["aid"])->find();
                                    $nums[] = [
                                        "sub_id" => (int)$admin["aid"],
                                        "sub_name" => $admin["name"],
                                        "sub_num" => (int)$value["number"]
                                    ];
                                }
                            }
                            Code(200, "绑定成功", $nums);
                        }else{
                            Code(403, "您不是该代理下的vip，请前去交保证金");
                        }
                    }else{
                        Code(403, "秘钥错误");
                    }
                }else{
                    Code(403, "该号码已经被其他用户绑定");
                }
            }else{
                Code(403, "您已经绑定");
            }
        }else{
            Code(403, "服务器错误，请稍后再试");
        }
    }

    // 获取状态
    public function GetMoney(){
        $id = $this->token["id"];
        $user = Db::name("user_buy")->where("ub_id", $id)->find();
        $data = [
            "money" => $user["pay_state"] == 0 ? "" : (int)$user["money"],
            "mstate" => (int)$user["pay_state"]
        ];

        Code(200, false, $data);
    }

    // 申请提现
    public function WithDraw(){
        $id = $this->token["id"];
        $user = Db::name("user_buy")->where("ub_id", $id)->find();
        switch ($user["pay_state"]){
            case 0: $array = [ "code" => 403, "msg" => "您没有资金可提现" ]; break;
            case 2: $array = [ "code" => 403, "msg" => "您已经申请了，请耐心等候" ]; break;
            case 3: $array = [ "code" => 403, "msg" => "您的资金正在被冻结，暂时无法提现" ]; break;
        }
        if($user["pay_state"] == 1){
            $up = [
                "pay_state" => 2
            ];
            Db::name("user_buy")->where("ub_id", $id)->update($up);
            $array = [
                "code" => 200,
                "data" => [
                    "mstate" => 2
                ],
                "msg" => "申请成功, 待管理员确认"
            ];
        }

        echo json_encode($array, 256);
    }

    public function GetConfig(){
        $array = [
            'code' => 200,
            'data' => [
                'tabbar' => true,
                'tabbarItems' => [
                    '专场图录',
                    '直播间',
                ],
                'goodsPrice' => [
                    '起拍价',
                    '当前价',
                    '成交价',
                ],
                'goodsState' => [
                    '未开拍',
                    '竞拍中',
                    '已停拍',
                ],
                'lotState' => [
                    '未开始',
                    '进行中',
                    '已结束',
                ],
                'fundMoney' => 2000,
                'lotItem' => [
                    'time' => '拍卖时间',
                    'start' => '开拍',
                    'goods' => '拍品',
                ],
                'index' => [
                    'tabs' => [
                        [
                            'name' => '近期专场',
                        ],
                        [
                            'name' => '历史专场',
                        ],
                    ],
                ],
                'goodsDetail' => [
                    'showConsign' => true,
                    'consign' => '委托出价',
                    'deadline' => '结算前截止委托',
                    'toast' => [
                        '关注成功，开拍时您将不会收到提醒',
                        '委托价格不得低于起拍价',
                    ],
                    'modal' => '请输入要委托的价格',
                ],
                'bid' => [
                    'toast' => '竞拍价格不得低于当前价',
                    'empty' => '等待管理员上架拍品',
                    'end' => '停止出价, 等待下一件拍品',
                    'chat' => [
                        'next' => '下口价',
                        'add' => '加一口',
                        'custom' => '自定义出价',
                        'buyer' => '成交人',
                        'bid' => '出价',
                    ],
                    'modal' => [
                        '本场拍卖已结束！',
                        '恭喜您竞得本件拍品！',
                    ],
                    'sign' => [
                        '开始出价',
                        '现场领先',
                        '网络领先',
                        '现场成交',
                        '网络成交',
                        '流拍',
                    ],
                ],
            ],
            'data1' => [
                'tabbar' => false,
                'tabbarItems' => [
                    '近期卖场',
                    '限时购',
                ],
                'goodsPrice' => [
                    '预售价',
                    '一口价',
                    '折后价',
                ],
                'goodsState' => [
                    '预售中',
                    '热卖中',
                    '已下架',
                ],
                'lotState' => [
                    '未开始',
                    '进行中',
                    '已结束',
                ],
                'fundMoney' => 2000,
                'lotItem' => [
                    'time' => '开售时间',
                    'start' => '开售',
                    'goods' => '商品',
                ],
                'index' => [
                    'tabs' => [
                        [
                            'name' => '近期卖场',
                        ],
                        [
                            'name' => '历史卖场',
                        ],
                    ],
                ],
                'goodsDetail' => [
                    'showConsign' => true,
                    'consign' => '尚未开售',
                    'deadline' => '线下结算',
                    'toast' => [
                        '关注成功',
                        '价格不能为空',
                    ],
                    'modal' => '请输入购买人用户号',
                ],
                'bid' => [
                    'toast' => '请输入合法内容',
                    'empty' => '暂无正在特卖的商品',
                    'end' => '特卖已结束',
                    'chat' => [
                        'next' => '赞',
                        'add' => '加1',
                        'custom' => '自定义',
                        'buyer' => '买家',
                        'bid' => '想买',
                    ],
                    'modal' => [
                        '本场已结束！',
                        '恭喜中奖！',
                    ],
                    'sign' => [
                        '开始',
                        '现场',
                        '网络',
                        '现场',
                        '网络',
                        '下架',
                    ],
                ],
            ],
        ];

        echo json_encode($array, 256);
    }

    public function GetSubs(){
        $admin = Db::name("admin")->where("role", 2)->select();
        $arr = [];
        if(!empty($admin)){
            foreach ($admin as $value){
                $arr[] = [
                    "value" => (string)$value["aid"],
                    "label" => $value["name"]
                ];
            }
        }

        Code(200, false, $arr);
    }

    public function GetNums(){
        $uid = $this->token["id"];
        $arr = [];
        $num = Db::name("num")->where("ub_id", $uid)->select();
        if(!empty($num)){
            foreach ($num as $value) {
                $admin = Db::name("admin")->where("aid", $value["aid"])->find();
                if(!empty($admin)){
                    $arr[] = [
                        "sub_id" => (int)$admin["aid"],
                        "sub_name" => $admin["name"],
                        "sub_num" => $value["number"]
                    ];
                }
            }
        }

        Code(200, false, $arr);
    }

    public function GetBaseInfo(){
        $uid = $this->token["id"];
        $user_info = Db::name("user_buy")->where("ub_id", $uid)->find();
        $data = [
            "address" => $user_info["address"],
            "bankcard" => $user_info["bankcard"],
            "bankname" => $user_info["bankname"],
            "country" => $user_info["country"],
            "email" => $user_info["email"],
            "idcard" => $user_info["idcard"],
            "idcardPhoto1" => $user_info["idcardPhoto1"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$user_info["ub_id"]}/".$user_info["idcardPhoto1"]."?t=".time(),
            "idcardPhoto2" => $user_info["idcardPhoto2"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$user_info["ub_id"]}/".$user_info["idcardPhoto2"]."?t=".time(),
            "job" => $user_info["job"],
            "line" => $user_info["line"],
            "name" => $user_info["name"],
            "passport" => $user_info["passport"] == "" ? "" : Config::get("app.ip")."static/upload/people/passport/buy/{$user_info["ub_id"]}/".$user_info["passport"]."?t=".time(),
            "photo" => $user_info["photo"] == "" ? "" : Config::get("app.ip")."static/upload/people/photo/buy/{$user_info["ub_id"]}/".$user_info["photo"]."?t=".time(),
            "postcode" => $user_info["postcode"],
            "tel1" => $user_info["tel1"],
            "tel2" => $user_info["tel2"],
            "wxid" => $user_info["wxid"],
        ];
        Code(200, false, $data);
    }

    public function UpdateBaseInfo(){
        $ub_id = $this->token["id"];
        $user = Db::name("user_buy")->where("ub_id", $ub_id)->find();

        $update = [
            "name" => isset($_POST["name"]) ? $_POST["name"] : $user["name"],
            "idcard" => isset($_POST["idcard"]) ? $_POST["idcard"] : $user["idcard"],
            "tel1" => isset($_POST["tel1"]) ? $_POST["tel1"] : $user["tel1"],
            "tel2" => isset($_POST["tel2"]) ? $_POST["tel2"] : $user["tel2"],
            "country" => isset($_POST["country"]) ? $_POST["country"] : $user["country"],
            "email" => isset($_POST["email"]) ? $_POST["email"] : $user["email"],
            "line" => isset($_POST["line"]) ? $_POST["line"] : $user["line"],
            "wxid" => isset($_POST["wxid"]) ? $_POST["wxid"] : $user["wxid"],
            "address" => isset($_POST["address"]) ? $_POST["address"] : $user["address"],
            "postcode" => isset($_POST["postcode"]) ? $_POST["postcode"] : $user["postcode"],
            "job" => isset($_POST["job"]) ? $_POST["job"] : $user["job"],
            "bankname" => isset($_POST["bankname"]) ? $_POST["bankname"] : $user["bankname"],
            "bankcard" => isset($_POST["bankcard"]) ? $_POST["bankcard"] : $user["bankcard"],
        ];


        if(isset($_FILES["idcardPhoto1"])){
            $update["idcardPhoto1"] = UploadFile($_FILES["idcardPhoto1"], $ub_id."_idcard1", "./static/upload/people/idcard/buy/$ub_id/", true);
        }
        if(isset($_FILES["idcardPhoto2"])){
            $update["idcardPhoto2"] = UploadFile($_FILES["idcardPhoto2"], $ub_id."_idcard2", "./static/upload/people/idcard/buy/$ub_id/", true);
        }
        if(isset($_FILES["photo"])){
            $update["photo"] = UploadFile($_FILES["photo"], $ub_id."_photo", "./static/upload/people/photo/buy/$ub_id/", true);
        }
        if(isset($_FILES["passport"])){
            $update["passport"] = UploadFile($_FILES["passport"], $ub_id."_passport", "./static/upload/people/passport/buy/$ub_id/", true);
        }

        Db::name("user_buy")->where("ub_id", $ub_id)->update($update);
        $user_info = Db::name("user_buy")->where("ub_id", $ub_id)->find();
        $data = [
            "address" => $user_info["address"],
            "bankcard" => $user_info["bankcard"],
            "bankname" => $user_info["bankname"],
            "country" => $user_info["country"],
            "email" => $user_info["email"],
            "idcard" => $user_info["idcard"],
            "idcardPhoto1" => $user_info["idcardPhoto1"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$user_info["us_id"]}/".$user_info["idcardPhoto1"]."?t=".time(),
            "idcardPhoto2" => $user_info["idcardPhoto2"] == "" ? "" : Config::get("app.ip")."static/upload/people/idcard/buy/{$user_info["us_id"]}/".$user_info["idcardPhoto2"]."?t=".time(),
            "job" => $user_info["job"],
            "line" => $user_info["line"],
            "name" => $user_info["name"],
            "passport" => $user_info["passport"] == "" ? "" : Config::get("app.ip")."static/upload/people/passport/buy/{$user_info["ub_id"]}/".$user_info["passport"]."?t=".time(),
            "photo" => $user_info["photo"] == "" ? "" : Config::get("app.ip")."static/upload/people/photo/buy/{$user_info["us_id"]}/".$user_info["photo"]."?t=".time(),
            "postcode" => $user_info["postcode"],
            "tel1" => $user_info["tel1"],
            "tel2" => $user_info["tel2"],
            "wxid" => $user_info["wxid"],
        ];
        Code(200, false, $data);
    }

    public function GetVips(){
        $ub_id = $this->token["id"];

        $user_number = Db::name("num")->where("ub_id", $ub_id)->select();
        $data = [];
        if(!empty($user_number)){
            foreach ($user_number as $value){
                $admin = Db::name("admin")->where("aid", $value["aid"])->find();
                $com = Db::name("user_buy_company")->where("aid", $value["aid"])->where("ub_id", $ub_id)->find();
                $data[] = [
                    "sub_id" => (int)$value["aid"],
                    "sub_name" => $admin["name"],
                    "sub_num" => $value["number"],
                    "sub_money" => $com["money"],
                    "sub_mstate" => (int)$com["pay_state"]
                ];
            }
        }

        Code(200, false, $data);

    }

}